//
//  XPPProjectItemSelection.m
//  XPPXcodePlugin
//
//  Created by yoyokko on 10-8-15.
//  Copyright 2010 yoyokko@gmail.com. All rights reserved.
//

#import "XPPProjectItemSelection.h"
#import "XPPXcodePlugin.h"
#import "GTMObjC2Runtime.h"
#import "XCProjectItemSelection.h"
#import "XCActionManager.h"
#import "PBXFileType.h"
#import "PBXFileReference.h"
#import "PBXExtendedOutlineView.h"
#import "XPPAutoAccessorizer.h"
#import "XPPAutoCompleteHelper.h"

static PBXFileReference *currentSelectFile = nil;

@implementation XPPProjectItemSelection

// Register our class to perform the swizzle once the plugin has finished
// loading.
+ (void)load {
	NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
	[XPPXcodePlugin registerSwizzleClass:self];
	[pool release];
}

- (id)init {
	if ((self = [super init])) {
		swizzleMethods([self class], [XCProjectItemSelection class], @selector(xpp_validateMenuItem_delete:), @selector(validateMenuItem_delete:));
		swizzleMethods([self class], [XCProjectItemSelection class], @selector(xpp_initWithSource:projectDocument:items:paths:), @selector(initWithSource:projectDocument:items:paths:));
		[self addAutoAccessorizerMethod];
	}
	return self;
}

- (void) addAutoAccessorizerMethod
{
	SEL ourSelector = @selector(xpp_validateMenuItem_autoAccessorizer:);
	Method ourMethod = class_getInstanceMethod([self class], ourSelector);
	class_addMethod([XCProjectItemSelection class],
					ourSelector,
					method_getImplementation(ourMethod),
					method_getTypeEncoding(ourMethod));

	ourSelector = @selector(autoAccessorizer:);
	ourMethod = class_getInstanceMethod([self class], ourSelector);
	class_addMethod([XCProjectItemSelection class],
					ourSelector,
					method_getImplementation(ourMethod),
					method_getTypeEncoding(ourMethod));
}

- (int)xpp_validateMenuItem_delete:(id)fp8
{
	int ret = [self xpp_validateMenuItem_delete:fp8];
	return ret;
}

- (int) xpp_validateMenuItem_autoAccessorizer:(NSMenuItem *) item
{
	PBXExtendedOutlineView *projectFileOutlienView = [[XPPAutoCompleteHelper sharedXPPAutoCompleteHelper] theProjectGroupOutlineView];
	// first try to get the outline view.
	if (projectFileOutlienView)
	{
		// if we got it. then get the selected items.
		// if the selected items contains one object which is not a source file, then return 0.
		NSArray *selectedItems = [projectFileOutlienView selectedItems];
		for (PBXFileReference *fileReference in selectedItems) {
			if ([fileReference isGroup] || ![[fileReference fileType] isSourceCode]){
				return 0;
			}
			currentSelectFile = fileReference;
		}
		// otherwise, set the target of menu to self.
		if ([[currentSelectFile name] hasSuffix:@".h"])
		{
			item.target = self;
			return 1;
		}
		return 0;
	}
	else 
	{
		// if there is no outline view in current window.
		// the try to get the first tiem return from XCActionManager.
		PBXFileReference *fileReference = [self firstItem];
		if ([fileReference isGroup]) {
			return 0;
		}else {
			PBXFileType *fileType = [fileReference fileType];
			if ([fileType isSourceCode] && [[fileReference name] hasSuffix:@".h"]) {
				item.target = self;
				currentSelectFile = fileReference;
				return 1;
			}
			return 0;
		}
		
		return 0;
	}	
}

- (void) autoAccessorizer:(id) sender
{
	XPPAutoAccessorizer *accessorizer = [[XPPAutoAccessorizer alloc] init];
	
	accessorizer.classNames = [[XPPAutoCompleteHelper sharedXPPAutoCompleteHelper] classNamesInSourceFile:currentSelectFile];
	
	[accessorizer.mainWindow center];
	[NSApp runModalForWindow:accessorizer.mainWindow];
	
	[accessorizer release];
}

- (id)xpp_initWithSource:(id)fp8 projectDocument:(id)fp12 items:(id)fp16 paths:(id)fp20
{
	return [self xpp_initWithSource:fp8 projectDocument:fp12 items:fp16 paths:fp20];
}

@end
